package com.zh.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zh.common.vo.PageDTO;
import com.zh.common.vo.PageQuery;
import com.zh.common.vo.Result;
import com.zh.modules.app.entity.IpProxyDO;
import com.zh.modules.app.param.ChangeIsAutoRenewedParam;
import com.zh.modules.app.param.ChangeRemarkParam;
import com.zh.modules.app.param.IpProxyQueryParam;
import com.zh.modules.app.repository.IpProxyRepository;
import com.zh.modules.app.utils.Condition;
import com.zh.modules.app.utils.JwtUtils;
import com.zh.modules.app.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app/ipProxy")
@Api(tags = "静态IP")
@Slf4j
public class IpProxyController {

    @Autowired
    private IpProxyRepository ipProxyRepository;

    @PostMapping("/query")
    @ApiOperation("列表查询")
    public Result<PageDTO<IpProxyDO>> query(@RequestBody IpProxyQueryParam param) {
        Integer userId = JwtUtils.getUser().getId();
        // 创建分页对象
        PageQuery query = new PageQuery();
        query.setPageSize(param.getPageSize());
        query.setPageNo(param.getPageNo());
        LambdaQueryWrapper<IpProxyDO> queryWrapper = new LambdaQueryWrapper<IpProxyDO>().eq(IpProxyDO::getUserId, userId);
        IPage<IpProxyDO> ipProxyDOPage = ipProxyRepository.page(Condition.getPage(query), queryWrapper);
        return Result.succeed(PageUtils.convert2PageDTO(ipProxyDOPage, null));
    }

    @PostMapping("/changeIsAutoRenewed")
    @ApiOperation("修改是否自动续费")
    public Result<Boolean> changeIsAutoRenewed(@RequestBody ChangeIsAutoRenewedParam param) {
        IpProxyDO ipProxyDO = ipProxyRepository.getById(param.getId());
        if (ipProxyDO == null) {
            return Result.failed("error", "更新失败", null);
        }
        Integer userId = JwtUtils.getUser().getId();
        if (!ipProxyDO.getUserId().equals(ipProxyDO.getUserId())) {
            log.error("所属用户ID不相等");
            return Result.failed("error", "更新失败", null);
        }
        ipProxyDO.setIsAutoRenewed(param.getStatus() ? 1 : 0);
        ipProxyRepository.updateById(ipProxyDO);
        return Result.succeed(true);
    }


    @PostMapping("/updateRemark")
    @ApiOperation("修改是否自动续费")
    public Result<Boolean> updateRemark(@RequestBody ChangeRemarkParam param) {
        IpProxyDO ipProxyDO = ipProxyRepository.getById(param.getId());
        if (ipProxyDO == null) {
            return Result.failed("error", "更新失败", null);
        }
        if (!ipProxyDO.getUserId().equals(ipProxyDO.getUserId())) {
            log.error("所属用户ID不相等");
            return Result.failed("error", "更新失败", null);
        }
        ipProxyDO.setRemark(param.getRemark());
        ipProxyRepository.updateById(ipProxyDO);
        return Result.succeed(true);
    }
}
